﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrenzDenWebSite.Business;

namespace FrenzDenWebSite
{
    public partial class FormJoin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadYear();
            }
        }

        private void LoadYear()
        {
            int endYear = DateTime.Now.Year - 10;
            int startYear = endYear - 80;

            for (int i = endYear; i >= startYear; i--)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    int profileCount = dataContext.ProfileInfos.Where(obj => (obj.EmailID == txtEmail.Text.Trim())).Count();
                    if (profileCount > 0)
                    {
                        msgbox = new MessageBox("Email ID already registered with us.");
                    }
                    else
                    {
                        ProfileInfo profile = new ProfileInfo()
                        {
                            EmailID = txtEmail.Text.ToLower().Trim(),
                            FullName = txtName.Text,
                            Password = txtPassword.Text,
                            BirthDay = Convert.ToDateTime(string.Format("{0}-{1}-{2}", ddlYear.SelectedValue, ddlMonth.SelectedValue, ddlDate.SelectedValue)),
                            Gender = ddlGender.SelectedValue,
                            CreatedDate = DateTime.Now,
                            ModifiedDate = DateTime.Now,
                            LastLoggedInDate = DateTime.Now,
                            ProfilePicID = ddlGender.SelectedValue == "Male" ? 1 : 2
                        };

                        dataContext.ProfileInfos.InsertOnSubmit(profile);
                        dataContext.SubmitChanges();
                        msgbox = new MessageBox("Account created successfully");
                    }
                }
            }
            catch
            {
                msgbox = new MessageBox("Error while creating account.");
            }

            msgboxpanel.InnerHtml = msgbox.ReturnObject();
        }
    }
}